﻿using FluentValidation;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using WebApplication2.model;

namespace WebApplication2.Attribute
{
    public static class MyCustomValidators
    {
        public static IRuleBuilderOptions<T, IList<TElement>> ListMustContainFewerThan<T, TElement>(this IRuleBuilder<T, IList<TElement>> ruleBuilder, int num)
        {
            return ruleBuilder.Must(list => list.Count < num).WithMessage("The list contains too many items");
        }
    }
    public class CustomerValidator : BaseValidator<TestMode>
    {
        public CustomerValidator()
        {

            RuleFor(x => x.Number)
                .NotEmpty()
                .WithMessage("Number 不能为空")
                .Must(NumberMust)
                .WithMessage("小于10");

            //RuleFor(x => x.id).ListMustContainFewerThan(10);
            //RuleFor(t => t).SetInheritanceValidator(t =>
            //{
            //    t.Add<BaseModel>(new BaseValidator());
            //});
            RuleFor(x => x.ProductId).NotEmpty().WithMessage("ProductId  不能为空");

            //RuleFor(x=>x.id).SetValidator(RuleFor(x=>x.id))
        }
       
        private bool NumberMust(int postcode)
        {
            if (postcode<10)
            {
                return false;
            }
            return true;
            // custom postcode validating logic goes here
        }
    }
}
